/* 
 * Copyright (C) 2012 Martin Řehánek <Martin.Rehanek at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

goog.provide('kramerius.audio.ui.playlist.Item');

goog.require('goog.dom');
goog.require('goog.events');
goog.require('goog.ui.Tooltip');


/** @const */ 
var ITEM_STATUS_NORMAL = 'playlistItem';
/** @const */ 
var ITEM_STATUS_ACTIVE = 'playlistItem playlistItemActive';
/** @const */ 
var ITEM_STATUS_SELECTED = 'playlistItem playlistItemSelected';
/** @const */ var MAX_TITLE_LENGTH = '45';


/**
 * Visual component wrapping the track.
 * @constructor 
 * @param {kramerius.audio.Track} track Track to be wrapped by Item.
 * @param {Boolean} selected if item should be selected
 */
kramerius.audio.ui.playlist.Item = function(track, selected){
    this.track_ = track;
    var itemClass = selected ? ITEM_STATUS_SELECTED:ITEM_STATUS_NORMAL;
    this.rootElement_ = this.createDom_(itemClass);
    this.playlist_ = undefined;
}

kramerius.audio.ui.playlist.Item.prototype.getDom = function(){
    return this.rootElement_;
}


kramerius.audio.ui.playlist.Item.prototype.setPlaylist = function(playlist){
    this.playlist_ = playlist;
}


kramerius.audio.ui.playlist.Item.prototype.createDom_ = function(itemClass){
    //root element
    var rootEl = goog.dom.createDom('div',{
        'class':itemClass
    });
    this.appendTrackDescription_(rootEl);
    this.appendButtons_(rootEl);
    return rootEl;
}

kramerius.audio.ui.playlist.Item.prototype.appendTrackDescription_ = function(rootEl){
    //container
    var containerEl = goog.dom.createDom('span',{
        'class':'playlistItemDescription unselectable'
    });
    //TODO: proper long description
    var longDescription = this.track_.getTitle() + ' (' +this.track_.getLength() + ')';
    new goog.ui.Tooltip(containerEl, longDescription);
    var thisItem = this;
    goog.events.listen(containerEl, goog.events.EventType.DBLCLICK,
        function (e) {
            thisItem.playThisItem();
        });
    goog.events.listen(containerEl, goog.events.EventType.CLICK,
        function (e) {
            thisItem.selectThisItem();
        });
    rootEl.appendChild(containerEl);
    
    //title
    var titleEl = goog.dom.createDom('span',{
        'class':'playlistItemTitle unselectable'
    });
    var titleShort = this.shortenTitle(this.track_.getTitle());
    titleEl.appendChild(document.createTextNode(titleShort));
    containerEl.appendChild(titleEl);
    
    //length
    var lengthEl = goog.dom.createDom('span',{
        'class':'playlistItemLength unselectable'    
    });
    lengthEl.appendChild(document.createTextNode(this.track_.getLength()));   
    containerEl.appendChild(lengthEl);
}


kramerius.audio.ui.playlist.Item.prototype.appendButtons_ = function(rootEl){
    var thisItem = this;
    //button "remove from playlist"    
    var removeEl = goog.dom.createDom('span',{
        'class':'playlistBtnItemRemove'
    });
    goog.events.listen(removeEl, goog.events.EventType.CLICK,
        function (e) {
            thisItem.removeFromPlaylist();
        });
    rootEl.appendChild(removeEl);
    //TODO: i18n
    new goog.ui.Tooltip(removeEl, "odstranit ze seznamu skladeb");
}

kramerius.audio.ui.playlist.Item.prototype.shortenTitle = function(title){
    if (title.length > MAX_TITLE_LENGTH){
        return title.substr(0, MAX_TITLE_LENGTH) + " ...";
    }else{
        return title;
    }
}

kramerius.audio.ui.playlist.Item.prototype.getTrack = function(){
    return this.track_;
}

kramerius.audio.ui.playlist.Item.prototype.moveUpInPlaylist = function(){
    var olderSibling = goog.dom.getPreviousElementSibling(this.rootElement_);
    if (olderSibling!= undefined){
        goog.dom.insertSiblingBefore(this.rootElement_, olderSibling);    
        this.playlist_.switchItemWithPrevious(this);
    }
}

kramerius.audio.ui.playlist.Item.prototype.moveDownInPlaylist = function(){
    var youngerSibling = goog.dom.getNextElementSibling(this.rootElement_);
    if (youngerSibling != undefined){
        goog.dom.insertSiblingAfter(this.rootElement_, youngerSibling);    
        this.playlist_.switchItemWithNext(this);
    }
}

kramerius.audio.ui.playlist.Item.prototype.removeFromPlaylist = function(){
    goog.dom.removeNode(this.rootElement_);
    this.playlist_.removeItem(this);
//TODO: pokud hraje, zastavit prehravani
// a mozna aktivovat item pod ním
}


kramerius.audio.ui.playlist.Item.prototype.setRootElementClass = function(elementClass){
    this.rootElement_.setAttribute("class", elementClass);
}

kramerius.audio.ui.playlist.Item.prototype.setStatusNormal = function(){
    this.setRootElementClass(ITEM_STATUS_NORMAL);
}

kramerius.audio.ui.playlist.Item.prototype.setStatusSelected = function(){
    this.setRootElementClass(ITEM_STATUS_SELECTED);
}

kramerius.audio.ui.playlist.Item.prototype.setStatusActive = function(){
    this.setRootElementClass(ITEM_STATUS_ACTIVE);
}

kramerius.audio.ui.playlist.Item.prototype.getStatus = function(){
    var rootElementClass = this.rootElement_.className;
    switch(rootElementClass){
        case ITEM_STATUS_ACTIVE:
            return "ACTIVE";
        case ITEM_STATUS_NORMAL:
            return "NORMAL";
        case ITEM_STATUS_SELECTED:
            return "SELECTED";
        default:
            return undefined;
    }
}

kramerius.audio.ui.playlist.Item.prototype.playThisItem = function(){
    this.playlist_.playItem(this);
}

kramerius.audio.ui.playlist.Item.prototype.selectThisItem = function(){
    this.playlist_.selectItem(this);
}

kramerius.audio.ui.playlist.Item.prototype.statusIsActive = function(){
    return this.getStatus() == "ACTIVE";
}

kramerius.audio.ui.playlist.Item.prototype.statusIsSelected = function(){
    return this.getStatus() == "SELECTED";
}

kramerius.audio.ui.playlist.Item.prototype.statusIsNormal = function(){
    return this.getStatus() == "NORMAL";
}

kramerius.audio.ui.playlist.Item.prototype.getId = function(){
    return this.track_.getId();
}

